
#include "member_BObject.h"

using namespace BWE;

member_BObject::member_BObject(BObject* object)
{
	boss = object;
	able = true;
	mutex_initialized = false;
}
member_BObject::~member_BObject()
{
	SigProcMap& gspm = this->globalSigProcMap();
	for (auto it = gspm.begin(); it != gspm.end(); it++)
	{
		(*it).remove(boss);
	}
	if(mutex_initialized)
	{
#ifdef _LINUX_
		pthread_mutex_destroy(&mutex);
#else
		DeleteCriticalSection(&criticalSection);
#endif
	}
}

SigProcMap& member_BObject::globalSigProcMap()
{
	static SigProcMap global_signal_proc_map;
	return global_signal_proc_map;
}
StrSigProcMap& member_BObject::globalStrSigProcMap()
{
	static StrSigProcMap global_string_signal_proc_map;
	return global_string_signal_proc_map;
}

bool member_BObject::initMutex()
{
	if(!mutex_initialized)
	{
#ifdef _LINUX_
		int error = pthread_mutex_init(&mutex, 0);
		mutex_initialized = error == 0;
#endif
#ifdef _WINDOWS_SYSTEM_
		InitializeCriticalSection(&criticalSection);
		mutex_initialized = true;
#endif
	}
	return mutex_initialized;
}

void member_BObject::emit(int signal, const BValue& value)
{
	boss->emit(signal, value);
}

